package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class addtype
 */
@WebServlet("/addtype")
public class addtype extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addtype() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: addtype").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String newtypename = request.getParameter("newtypename");
		String newtypenameps = request.getParameter("newtypenameps");
		System.out.println("newtypename:" + newtypename);
		System.out.println("newtypenameps:" + newtypenameps);
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection cont1;
		try {
			cont1 = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			String charuntn = "insert into blog_type (type_name,remark) values(?,?)";
			PreparedStatement prestn1 = cont1.prepareStatement(charuntn);
			prestn1.setString(1, newtypename);
			prestn1.setString(2, newtypenameps);
			if(!newtypename.equals("")){
				prestn1.executeUpdate();
			}
			prestn1.close();
			cont1.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect("/admin_typegl.jsp");
	}

}
